package com.oocl.client.ui.impl;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.oocl.client.callback.ChangeFrameCallback;
import com.oocl.client.callback.ProtocolFinishProcessCallback;
import com.oocl.client.process.ProtocolReceiver;
import com.oocl.client.process.ProtocolSender;
import com.oocl.client.ui.BaseFrame;
import com.oocl.client.ui.IChatOperator;
import com.oocl.client.ui.IRegister;
import com.oocl.client.util.Logger;
import com.oocl.qq.protocol.Action;
import com.oocl.qq.protocol.Protocol;

public class RegisterFrame extends BaseFrame implements ActionListener,
		IRegister {
	private JTextField nameTf;
	private JPasswordField psdPf;
	private JPasswordField rePsdPf;
	private JButton cancelBtn;
	private JButton registerBtn;

	private LoginFrame loginFrame;

	private String name;

	private ChangeFrameCallback mChangeFrameCallback;
	// /协议发送接口
	private ProtocolSender protocolSender;

	public RegisterFrame(LoginFrame loginFrame) {
		this.loginFrame = loginFrame;
	}

	public void setProtocolSender(ProtocolSender protocolSender) {
		this.protocolSender = protocolSender;
	}

	public void setChangeFrameCallback(ChangeFrameCallback changeFrameCallback) {
		this.mChangeFrameCallback = changeFrameCallback;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String inputName = this.nameTf.getText();
		char[] psdStr = this.psdPf.getPassword();
		char[] rePsdStr = this.rePsdPf.getPassword();

		if (psdStr.length != rePsdStr.length) {
			Logger.log("两次输入密码不一致！");
			int option = JOptionPane.showConfirmDialog(this, "两次输入密码不一致！",
					"提示 ", JOptionPane.OK_OPTION);
			this.rePsdPf.setText("");
			this.psdPf.setText("");
		} else {
			boolean isRight = true;
			for (int i = 0; i < psdStr.length; i++) {
				if (psdStr[i] != rePsdStr[i]) {
					isRight = false;
					break;
				}
			}
			if (!isRight) {
				Logger.log("两次输入密码不一致！");
				int option = JOptionPane.showConfirmDialog(this, "两次输入密码不一致！",
						"提示 ", JOptionPane.OK_OPTION);
				this.rePsdPf.setText("");
				this.psdPf.setText("");
				return;
			}

			this.name = inputName;
			Logger.log("注册用户名： " + inputName + "， 密码： " + psdStr);
			Protocol p = new Protocol(Action.Register, this.name);
			p.setMsg(new StringBuilder().append(psdStr).toString());
			p.setTime(System.currentTimeMillis());
			sendProtocol(p);
		}

	}

	@Override
	protected void initFrame() {
		this.setSize(400, 300);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("QQ客户端注册");
	}

	@Override
	protected void init() {
		this.setLayout(null);
		nameTf = new JTextField();
		psdPf = new JPasswordField();
		rePsdPf = new JPasswordField();
		cancelBtn = new JButton("cancel");
		registerBtn = new JButton("Register");
		JLabel namel = new JLabel("新用户名：");
		JLabel psdl = new JLabel("密码：");
		JLabel psd2 = new JLabel("重复密码：");
		namel.setBounds(50, 20, 100, 40);
		psdl.setBounds(30, 80, 120, 40);
		psd2.setBounds(30, 140, 120, 40);
		nameTf.setBounds(120, 20, 260, 40);
		psdPf.setBounds(120, 80, 260, 40);
		rePsdPf.setBounds(120, 140, 260, 40);
		registerBtn.setBounds(40, 200, 90, 40);
		cancelBtn.setBounds(260, 200, 90, 40);

		nameTf.setFont(new Font("宋体", Font.BOLD, 18));

		this.add(namel);
		this.add(psdl);
		this.add(psd2);
		this.add(nameTf);
		this.add(psdPf);
		this.add(rePsdPf);
		this.add(registerBtn);
		this.add(cancelBtn);
	}

	@Override
	protected void addEvent() {
		registerBtn.addActionListener(this);
		cancelBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (mChangeFrameCallback != null)
					mChangeFrameCallback.changeFrame(loginFrame);
			}
		});
	}

	@Override
	protected void sendProtocol(Protocol protocol) {
		this.protocolSender.sendProtocol(protocol);
	}

	@Override
	public void registerFail(String msg) {
		// /注册失败
		Logger.log("注册失败！" + msg);
		int option = JOptionPane.showConfirmDialog(this, msg, "提示 ",
				JOptionPane.OK_OPTION);
		this.psdPf.setText("");
		this.rePsdPf.setText("");
		this.nameTf.setText("");
	}

	@Override
	public void finishRegister(String userName, List<String> userList) {
		// /这个时候，表示登录成功了， 应该切换到 ChatFrame
		System.out.println("注册成功！！！现在就新建chatFrame");
		ChatFrame chatFrame = new ChatFrame(userName, userList);
		chatFrame.setProtocolSender(this.protocolSender);
		if (this.mChangeFrameCallback != null)
			this.mChangeFrameCallback.changeFrame(chatFrame);
	}
}
